//
//  ZJRoomModel.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/12.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJRoomModel.h"
static NSString * const kRoomTableName = @"room_table";
@implementation ZJRoomModel
/**
 *  获取单个商品加入购物车个数
 *  shopID:商品id
 */
+ (NSInteger)numberWithRoomID:(NSString *)roomID
{
    NSInteger number = 0;
    NSString *uid = allConstMemberid;
    NSArray *resultArray = [FMDBHelper query:kRoomTableName where:[NSString stringWithFormat:@"uid=%@ and house_id='%@'", uid, roomID], nil];
    if (resultArray.count>0) {
        ZJRoomModel *model = [[ZJRoomModel alloc] initWithDictionary:[resultArray firstObject]];
        number = [model.tempnumber integerValue];
    }
    
    return number;
}
/**
 *  添加户型到抢购
 *  shopModel:商品对象
 */
+ (NSInteger)addRoomWithRoomModel:(ZJRoomModel *)roomModel
{
    NSString *uid = allConstMemberid;
    NSString *pkey = [NSString stringWithFormat:@"%@%@", uid, roomModel.house_id];
    
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(pkey char(100) PRIMARY KEY, house_name char(100), pic_url char(100), layout char(100), house_area char(100), actual_area char(100), love_count char(100), model_name char(100), uid char(100), house_id char(100), pic_ratio char(100), house_orientation char(100), balcony_orientation char(100), status char(100), tempnumber char(100), ceiling_name char(100), unit_name char(100), orientation_pic char(100), unit_unit char(100), building_name char(100), partition_name char(100), building_unit char(100), house_area_circle char(100))", kRoomTableName];
    BOOL ok = [FMDBHelper createTable:sql];
    if (!ok) {
        return 0;
    }
    
    NSMutableDictionary *dict = roomModel.mj_keyValues.mutableCopy;
    dict[@"pkey"] = pkey;
    dict[@"uid"] = uid;
    
    [dict removeObjectForKey:@"type"];
    
    BOOL insert = [FMDBHelper insert:kRoomTableName keyValues:dict];
    if (!insert) {

        return 0;
    }
    
    return [roomModel.tempnumber integerValue];
}
/**
 *  获取钱够户型所有数据
 */
+ (NSMutableArray<ZJRoomModel *> *)rooms
{
    NSString *uid = allConstMemberid;
    NSArray *resultArray = [FMDBHelper query:kRoomTableName where:[NSString stringWithFormat:@"uid=%@", uid], nil];
    NSMutableArray *goods = [NSMutableArray arrayWithCapacity:resultArray.count];
    for (NSDictionary *dict in resultArray) {
        ZJRoomModel *model = [[ZJRoomModel alloc] initWithDictionary:dict];
        [goods addObject:model];
    }
    
    return goods;
}
/**
 *  删除指定户型
 *  roomModels:商品对象数组
 */
+ (BOOL)removeRoomModel:(NSArray<ZJRoomModel *> *)roomModels
{
    NSString *uid = allConstMemberid;
    
    NSMutableArray *sqls = [NSMutableArray arrayWithCapacity:roomModels.count];
    for (ZJRoomModel *model in roomModels) {
        NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@",kRoomTableName, [NSString stringWithFormat:@"uid=%@ and house_id='%@'", uid, model.house_id]];
        [sqls addObject:sql];
    }
    BOOL isFinish= [FMDBHelper executeBatch:sqls useTransaction:YES];
    
    return isFinish;
}

@end
